#====# Appendix B: FCPA histories and reputational risk rating #====#
# WARNING: This code estimates two models (for Figure B.2) that are computationally very expensive and take about 5min each to estimate

# Load libraries and set defaults ----
library(fixest)
library(modelsummary)
library(tinytable)
library(lemon)
library(tictoc)
library(tidyverse)
library(tidylog, warn.conflicts = FALSE)
source("aux/plot_theme.R")

# Import data ----
reprisk_cs <- read_rds("data/reprisk_cs.rds") # reprisk data (cross-section)
reprisk_pd <- read_rds("data/reprisk_pd.rds") # reprisk data (panel data)

# Table B.1: Cross-sectional difference in reputational risk score between past FCPA targets and placebos ----
mod <- feols(current_rri ~ FCPA_sample,
             data = reprisk_cs,
             vcov = "hetero")

mod_match <- feols(current_rri ~ FCPA_sample,
                   data = reprisk_cs %>%
                     filter(matched == 1),
                   vcov = "hetero")

rows <- tribble(~term, ~mod, ~mod_match,
                "Std.Errors", "White-robust", "White-robust")
attr(rows, "position") <- c(5)

modelsummary(list(mod, mod_match),
             stars = c("*" = 0.05),
             escape = FALSE,
             coef_map = c("FCPA_sample" = "Past FCPA target",
                          "(Intercept)" = "(Intercept)"),
             gof_omit = "R2 Within|AIC|BIC|RMSE|FE|Std.Errors",
             notes = paste0("Linear regression models of the latest available score (Dec 31 2023) proposed by RepRisk to measure reputational risk (RRI). ",
                            "Higher values indicate higher reputational risk. Past FCPA targets and matched placebo firms are considered. ",
                            "``Past FCPA target'' is a binary taking value of 1 for firms that have been targets of an FCPA ",
                            "investigation or enforcement action in the past. Heteroskedasticity White-robust standard errors in parentheses. ",
                            collapse = ""),
             title = "Cross-sectional difference in RepRisk reputational risk score for past FCPA targets and matched placebo firms \\label{tab:rri_cs}",
             add_rows = rows) %>%
  theme_tt("resize", width = .9) %>%
  theme_tt("placement", latex_float = "!htbp") %>%
  group_tt(j = list("All past targets" = 2,
                    "Only matched targets" = 3)) %>%
  save_tt("tables/table_B1.html", overwrite = TRUE)

# Figure B.1: DiD of firms’ reputational risk following an FCPA enforcement action (1mo) ----
mod_1mo <- feols(current_rri ~ i(FCPA_time_to, FCPA_sample, ref = -1) | ticker_symbol + date,
                 data = reprisk_pd %>%
                   filter(FCPA_time_to >= -30 &
                            FCPA_time_to <= 30))

mod_1mo_matched <- feols(current_rri ~ i(FCPA_time_to, FCPA_sample, ref = -1) | ticker_symbol + date,
                         data = reprisk_pd %>%
                           filter(matched == 1) %>%
                           filter(FCPA_time_to >= -30 &
                                    FCPA_time_to <= 30))

mod_1mo %>%
  broom::tidy(conf.int = TRUE) %>%
  mutate(term = term %>%
           str_remove("^.+\\:\\:") %>%
           str_remove("\\:.+$") %>%
           as.numeric()) %>%
  bind_rows(data.frame(term = -1, estimate = 0)) %>%
  mutate(type = "Compare all past FCPA defendants with matched placebo firms") %>%
  rbind(mod_1mo_matched %>%
          broom::tidy(conf.int = TRUE) %>%
          mutate(term = term %>%
                   str_remove("^.+\\:\\:") %>%
                   str_remove("\\:.+$") %>%
                   as.numeric()) %>%
          bind_rows(data.frame(term = -1, estimate = 0)) %>%
          mutate(type = "Compare matched past FCPA defendants with matched placebo firms")) %>%
  ggplot(aes(x = term, y = estimate)) +
  geom_pointrange(aes(ymin = conf.low, ymax = conf.high)) +
  geom_hline(yintercept = 0, linewidth = 0.1) +
  geom_vline(xintercept = -0.5, linewidth = 0.1) +
  ylab("Estimated ATT of FCPA enforcement on\ntarget firms' reputational risk score (RepRisk RRI)") +
  xlab("Days to FCPA enforcement action (t = 0)") +
  facet_rep_wrap("type", nrow = 2, repeat.tick.labels = "all")
ggsave("plots/figure_B1.pdf", width = 8, height = 5)

# Figure B.2: DiD of firms’ reputational risk following an FCPA enforcement action (1yr) ----
# WARNING: These two models are computationally very expensive and take about 5min each to estimate
mem.maxVSize(vsize = Inf) # max memory size is needed for this beast to run (1+M obs, 200+ firm-FEs, and 6,000+ date-FEs)

tic()
mod_1yr <- feols(current_rri ~ i(FCPA_time_to, FCPA_sample, ref = -1) | ticker_symbol + date,
                 data = reprisk_pd) 
toc() # about 5 min

tic()
mod_1yr_matched <- feols(current_rri ~ i(FCPA_time_to, FCPA_sample, ref = -1) | ticker_symbol + date,
                         data = reprisk_pd %>%
                           filter(matched == 1)) 
toc() # about 5 min

mod_1yr %>%
  broom::tidy(conf.int = TRUE) %>%
  mutate(term = term %>%
           str_remove("^.+\\:\\:") %>%
           str_remove("\\:.+$") %>%
           as.numeric()) %>%
  bind_rows(data.frame(term = -1, estimate = 0)) %>%
  mutate(type = "Compare all past FCPA defendants with matched placebo firms") %>%
  rbind(mod_1yr_matched %>%
          broom::tidy(conf.int = TRUE) %>%
          mutate(term = term %>%
                   str_remove("^.+\\:\\:") %>%
                   str_remove("\\:.+$") %>%
                   as.numeric()) %>%
          bind_rows(data.frame(term = -1, estimate = 0)) %>%
          mutate(type = "Compare matched past FCPA defendants with matched placebo firms")) %>%
  ggplot(aes(x = term, y = estimate)) +
  geom_line() +
  geom_ribbon(aes(ymin = conf.low, ymax = conf.high),
              alpha = .3) +
  geom_hline(yintercept = 0, linewidth = 0.1) +
  geom_vline(xintercept = -0.5, linewidth = 0.1) +
  ylab("Estimated ATT of FCPA enforcement on\ntarget firms' reputational risk score (RepRisk RRI)") +
  xlab("Days to FCPA enforcement action (t = 0)") +
  facet_rep_wrap("type", nrow = 2, repeat.tick.labels = "all") +
  theme(plot.margin = unit(c(5.5, 8, 5.5, 5.5), "pt"))
ggsave("plots/figure_B2.pdf", width = 8, height = 5)

#====# The End #====#